package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.CargoExterno;

public class CargoExternoDaoImpl implements CargoExternoDao {
	Connection c;

	public CargoExternoDaoImpl() {
		GenericDao gDao = new GenericDao();
		c = gDao.getConnection();
	}

	@Override
	public void salvaCargoExterno(CargoExterno cargoExterno)throws SQLException {
		String sql = "INSERT INTO cargosExternos VALUES (?,?)";
		PreparedStatement ps = c.prepareStatement(sql);

		ps.setInt(1, cargoExterno.getIdCargExt());
		ps.setString(2, cargoExterno.getNomeCargExt());

		ps.execute();
		ps.close();

	}

	@Override
	public void editaCargoExterno(CargoExterno cargoExterno)
			throws SQLException {
		String sql = "UPDATE cargosExternos SET nomeCargExt = ? WHERE idCargExt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, cargoExterno.getNomeCargExt());
		ps.setInt(2, cargoExterno.getIdCargExt());
		ps.execute();
		ps.close();

	}

	@Override
	public void excluiCargoExterno(CargoExterno cargoExterno)
			throws SQLException {
		String sql = "DELETE cargosExternos WHERE idCargExt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, cargoExterno.getIdCargExt());
		ps.execute();
		ps.close();

	}

	@Override
	public CargoExterno pesquisaCargoExterno(CargoExterno cargoExterno)
			throws SQLException {
		CargoExterno ce = new CargoExterno();
		String sql = "SELECT idCargExt, nomeCargExt FROM cargosExternos WHERE idCargExt = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, cargoExterno.getIdCargExt());
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			ce.setIdCargExt(rs.getInt("idCargExt"));
			ce.setNomeCargExt(rs.getString("nomeCargExt"));

		}
		ps.close();
		rs.close();
		return ce;
	}

}
